/***************************************************************************
 *  Filename: messageparser.cpp
 *  Class: MessageParser
 *
 *  Copyright 2012  Xander van Kooten
 *  <xandervk@gmail.com>
 *
 *  Copyright 2012  Mario Henrique Voorsluys
 *  <forcaeluz@gmail.com>
 *
 ****************************************************************************/

/*
 *
 * This file is part of Parsax-Remote-Controller.
 *
 *    Parsax-Remote-Controller is free software: you can redistribute it
 *    and/or modify it under the terms of the GNU General Public License
 *    as published by the Free Software Foundation, either version 3 of the
 *    License, or (at your option) any later version.
 *
 *    Parsax-Remote-Controller is distributed in the hope that it will be useful,
 *    but WITHOUT ANY WARRANTY; without even the implied warranty of
 *    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *    GNU General Public License for more details.
 *
 *    You should have received a copy of the GNU General Public License
 *    along with Parsax-Remote-Controller.
 *    If not, see <http://www.gnu.org/licenses/>.
 */
#include "messageparser.h"

MessageParser::MessageParser(QObject *parent):QObject(parent)
{

}

void MessageParser::handleMessage(QString data){
    qDebug() << "[MessageParser] Parsing " << data;
    if(data.startsWith("#") && data.endsWith("##")){
        QStringList parts = data.split("#");
        parts = parts.mid(1, parts.size()-3);
        QString type = parts.at(0);
        if(type=="prg"){        // Program Type Messages
            if(parts.size()==3){
                QString id = parts.at(1);
                QString action = parts.at(2);
                if(action=="start"){
                    emit startProgramReceived(id);
                }else if(action=="stop"){
                    emit stopProgramReceived(id);
                }else if(action=="restart"){
                    /*! \todo Implement restart program signal */
                }else{
                    qWarning() << "[MessageParser] Unknown program function";
                }
            }else{
                qWarning() << "[MessageParser] Invalid number of arguments for \"prg\" messages";
            }
        }else if(type=="npr"){  // New program type Messages (only start)
            QString id = parts.at(1);
            QString program = parts.at(2);
            QStringList params = parts.mid(3);
            emit startProgramReceived(id, program, params);
        }else if(type=="clo"){  // CommandLineOutput type messages
            QString id = parts.at(1);
            int type = parts.at(2).toInt();
            QStringList msgLines = parts.mid(3);
            QString msg = msgLines.join("\n");
            emit cloReceived(id, type, msg);
        }else if(type=="sta"){  // Status type messages
            QString id = parts.at(1);
            QString status = parts.at(2);
            emit statusReceived(id, status);
        }else if(type=="cmd"){  // Command type message
            QString id = parts.at(1);
            QString cmd = parts.at(2);
            /*! \todo Implement the cmd handling */
        }else if(type=="cfg"){  // Configuration type messages
            QString action = parts.at(1);
            /*! \todo Implement the configuration message handling */
            if(action=="set"){

            }else if(action=="rld"){

            }else if(action=="rea"){

            }else if(action=="val"){

            }else if(action=="save"){

            }
        }else{                  // Not a valid message type.
            qWarning() << "[MessageParser] Unrecognized message type";
        }
    }
}

QByteArray MessageParser::configurationMessage(QMap<QString, QVariant> configuration){
    /*!
      \todo Implement.
      */
    return QByteArray();
}

QByteArray MessageParser::cloMessage(QString id, int type, QString msg){
    QString tmp;
    tmp.append("#clo#");
    tmp.append(id);
    tmp.append("#");
    tmp.append(QString::number(type));
    tmp.append("#");
    msg.replace("\n","#");
    if(msg.endsWith("#")){
        msg.chop(1);
    }
    tmp.append(msg);
    tmp.append("##");
    qDebug() << "[MessageParser] CLO message is: " << tmp;
    return QByteArray().append(tmp);
}

QByteArray MessageParser::statusMessage(QString id, QString status){
    QString tmp = "#sta#";
    tmp.append(id);
    tmp.append("#");
    tmp.append(status);
    tmp.append("##");
    qDebug() << "[MessageParser] Status message is: " << tmp;
    return QByteArray().append(tmp);
}

QByteArray MessageParser::nprMessage(QString id, QString program, QStringList params){
    QString tmp = "#npr#";
    tmp.append(id);
    tmp.append("#");
    tmp.append(program);

    if(!params.isEmpty()){
        tmp.append("#");
        QString paramsString = params.join("#");
        tmp.append(paramsString);
    }
    tmp.append("##");
    qDebug() << "[MessageParser] New program message is: " << tmp;
    return QByteArray().append(tmp);
}

QByteArray MessageParser::prgMessage(QString id, QString action){
    QString tmp = "#prg#";
    tmp.append(id);
    tmp.append("#");
    tmp.append(action);
    tmp.append("##");
    qDebug() << "[MessageParser] Program message is: " << tmp;
    return QByteArray().append(tmp);
}
